package lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class InterpretLock implements Runnable {
    public InterpretLock(Lock lock) {
        this.lock = lock;
    }

    Lock lock;

    @Override
    public void run() {


        try {
            lock.lock();
//            lock.lockInterruptibly();
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(10000);
            System.out.println(Thread.currentThread().getName()+"执行完成");

        } catch (InterruptedException e) {
            System.out.println("锁被中断了");
        }finally {
            lock.unlock();
            System.out.println("释放锁");
        }
    }

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Thread t1 = new Thread(new InterpretLock(lock), "线程1");
        Thread t2 = new Thread(new InterpretLock(lock), "线程2");
        t1.start();
        t1.interrupt();

        t2.start();



    }
}
